<?php
/**
 * Created by PhpStorm.
 * User: Administrator--Reer
 * Date: 2019/9/9 0009
 * Time: 17:17
 */

namespace app\common\controller;

use app\common\bean\GlobalBean;
use app\common\bean\StorePermissionBean;
use app\common\exception\MyException;
use app\common\manage\StoreRolePermissionManage;
use app\common\utils\PermissionUtil;
use think\App;

class StoreController extends BaseController
{
    /**
     * StoreController constructor.
     * @param App|null $app
     * @throws \Exception
     */
    public function __construct(App $app = null) {
        parent::__construct($app);
        if(!session(GlobalBean::$storeId)){
            MyException::throwMyException(GlobalBean::$goLogin);
        }
        if(!request()->isAjax()){
            $permissionStringArray = PermissionUtil::getInstance()->getSessionPermission(request()->module(),request()->controller());
            $this->assign('permissionStringArray',$permissionStringArray);
        }
        $this->checkActionPermission();
    }

    /**
     * 检测action权限
     */
    private function checkActionPermission(){
        if(session(GlobalBean::$storeRoleId) !== GlobalBean::$superStoreRoleId){//非超级场馆、后台管理员 验证权限
            $storePermissionBean = new StorePermissionBean();
            $storePermissionBean->setModel(request()->module());
            $storePermissionBean->setController(request()->controller());
            $storePermissionBean->setAction(request()->action());
            StoreRolePermissionManage::getInstance()->checkRolePermission(session(GlobalBean::$storeRoleId),$storePermissionBean);
        }
    }
}